package com.pet.shop.dto;

import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaowenbang on 2018/2/8.
 */
public class PageDto<T extends Serializable> implements Serializable {

    private static final long serialVersionUID = 108552937869216470L;

    private int page = 1;
    private int rows = 10;
    private int code;
    private String message;
    private String errorMsg;
    private Map<String, Object> params = new HashMap<String, Object>();
    private Map<String, Object> orderBy = new HashMap<String, Object>();
    private long totalRecord;
    private List<T> results;

    public void clearParams(){
        if(params != null) params.clear();
        if(orderBy != null) orderBy.clear();
    }

    public void setPageResult(Page<T> page){
        this.setTotalRecord(page.getTotal());
        this.setResults(page.getResult());
        clearParams();
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public Map<String, Object> getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(Map<String, Object> orderBy) {
        this.orderBy = orderBy;
    }

    public List<T> getResults() {
        if(results == null) results = new ArrayList<>();
        return results;
    }

    public void setResults(List<T> results) {
        this.results = results;
    }

    public long getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(long totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage(){
        return (int)Math.ceil((double)this.totalRecord / rows);
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
